package com.lyt.springboot.util.algorithm;

public class SS {

    public static void main(String[] args) {
        SS ss = new SS();
        int[][] num = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[][] num2 = {{1, 2, 8,9}, {4, 7,10,13}};
        int[][] num1 = {{}};
        ss.find2(7, num2);

    }

    public boolean find(int target, int[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                if (array[i][j] == target) {
                    System.out.println("i" + i + " " + "j" + j);
                    return true;
                }
            }
        }
        return false;
    }


    public boolean find2(int target, int[][] array) {
        if (array == null || array.length == 0 ||(array.length==1&&array[0].length==0)) {
            return false;
        }
        //定义右上角
        int row = 0;
        int colum = array[0].length - 1;

        //考虑边界异常 {{1,2,3}}  4    {{2},{3},{4}}  1
        if (array.length == 1) {
            if (array[0][array.length-1] < target) {
                return false;
            }
        }
        if (array[0].length == 1) {
            if (array[0][0] > target) {
                return false;
            }
        }
        while (row < array.length && colum >= 0) {
            int num = array[row][colum];

            if (target == num) {
                System.out.println("i" + row + " " + "j" + colum);
                return true;
            }
            if (target > num) {
                row++;

            }
            if (target < num) {
                colum--;
            }
        }
        return false;
    }
}
